package com.qzsoft.zhjd.machine.modules.machinereport.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.system.modules.permission.dept.dao.SysDeptMapper;
import com.qzsoft.system.repository.sys.entity.SysDeptEntity;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.machine.modules.machinereport.dto.DeptReportMaintenanceDto;
import com.qzsoft.zhjd.machine.modules.machinereport.dto.MachineReportMaintenanceDto;
import com.qzsoft.zhjd.machine.modules.machinereport.mapper.MachineReportMaintenanceMapper;
import com.qzsoft.zhjd.machine.modules.machinereport.service.MachineReportMaintenanceService;

/**
 * @ClassName ReportMaintenanceServiceImpl
 * @Description 维修保养统计报表业务接口实现
 * @author linhuawei
 * @since 2021-11-03
 */
@Service
public class MachineReportMaintenanceServiceImpl implements MachineReportMaintenanceService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private MachineReportMaintenanceMapper machineReportMaintenanceMapper;

    @Autowired
    private SysDeptMapper sysDeptDao;

    @Override
    public IPage<MachineReportMaintenanceDto> machine(Map<String, Object> params) {
        IPage<MachineReportMaintenanceDto> page = baseServiceComponent.getPage(params,
                MachineReportMaintenanceDto.class);
        page = machineReportMaintenanceMapper.machine(page, params);
        return ConvertDictUtils.formatDicDataPage(page);
    }

    @Override
    public List<DeptReportMaintenanceDto> dept(Map<String, Object> params) {
        List<DeptReportMaintenanceDto> dtos = new ArrayList<>();
        // 判断是否有机构主键
        UserDetail user = baseServiceComponent.basegetLoginUser();
        String deptId = user.getDeptId();
        if (null != params.get("deptId")) {
            String temp = params.get("deptId").toString();
            if (temp.startsWith(deptId)) {
                deptId = temp;
            }
        }
        params.put("deptId", deptId);

        // 获取当前机构
        SysDeptEntity dept = sysDeptDao.getRealId(deptId);
        DeptReportMaintenanceDto result = machineReportMaintenanceMapper.dept(params);
        if(null == result) {
            result = new DeptReportMaintenanceDto();
            result.setAmount(BigDecimal.ZERO);
            result.setMaintainAmount(BigDecimal.ZERO);
            result.setRepairAmount(BigDecimal.ZERO);
            result.setRepairCount(0);
            result.setMaintainCount(0);
        }
        result.setDeptId(dept.getId());
        result.setDeptPkid(dept.getPkid());
        result.setDeptName(dept.getName());

        // 获取下级机构
        QueryWrapper<SysDeptEntity> query = new QueryWrapper<>();
        query.eq("pid", deptId).orderByAsc("sort", "id");
        List<SysDeptEntity> deptList = sysDeptDao.selectList(query);
        List<DeptReportMaintenanceDto> list = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(deptList)) {
            DeptReportMaintenanceDto dto = null;
            list = new ArrayList<DeptReportMaintenanceDto>();
            for (int i = 0, len = deptList.size(); i < len; i++) {
                dept = deptList.get(i);
                if("4".equals(dept.getType())) {
                    continue;// 磅房类型不参与统计
                }
                params.put("deptId", dept.getId());
                dto = machineReportMaintenanceMapper.dept(params);
                if(null == dto) {
                    dto = new DeptReportMaintenanceDto();
                    dto.setAmount(BigDecimal.ZERO);
                    dto.setMaintainAmount(BigDecimal.ZERO);
                    dto.setRepairAmount(BigDecimal.ZERO);
                    dto.setRepairCount(0);
                    dto.setMaintainCount(0);
                }
                dto.setDeptId(dept.getId());
                dto.setDeptPkid(dept.getPkid());
                dto.setDeptName(dept.getName());
                dto.setList(null);
                list.add(dto);
            }
        }
        if (list.size()>0) {
            dtos.addAll(list);
        }
        dtos.add(result);
        // 返回结果
        return dtos;
    }

}
